//
//  MainViewController.swift
//  MZBluePrinter
//
//  Created by Angus on 15/11/30.
//  Copyright © 2015年 MZ. All rights reserved.
//

import UIKit

class MainViewController: UIViewController {
    
    @IBOutlet weak var connectButton: UIButton!
    
    @IBOutlet weak var textView: UITextView!
    
    //MARK: Life Cyc
    override func viewDidLoad() {
        super.viewDidLoad()
        
      
        showTip()
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        BLEManager.shareInstance().completion_status = {
            [unowned self] (status:BLEStatus) in
            
            self.removeAllOverlays()
            
            switch status {
            case .Disconnect:
                if BLEManager.shareInstance().isConnect() {
                    self.mz_showAlert(title: "Tip", message: "Printer is disconnected")
                }
                break
                
            case .PowerOff:
                self.mz_showAlert(title: "Tip", message: "Please turn on Bluetooth")
                break
            }
        }
        
        BLEManager.shareInstance().completion_error = { [unowned self] (error) in
            self.removeAllOverlays()
        }
        
        BLEManager.shareInstance().completion_Log = {
            [unowned self] (obj) in
            
            self.removeAllOverlays()
            
            let log:String = obj as! String
            self.textView.mz_appendText(log)
        }

    }
    
    func showTip() {
        let tip = " 使用说明 \n 1)确保手机蓝牙连接 \n 2)按'Scan'扫描打印机,并选择对应打印机进行连接 \n 3按'Disconnect' 断开打印机 \n 4)'test80' 测试80mm, 'test58' 测试58mm , 点击后稍等，等待打印机出纸"
        self.textView.text = tip
    }
    
    func testPrinter(printer: MazingPrinter) {
        
        if !BLEManager.shareInstance().isConnect() {
            self.mz_showAlert(title: "Please connect a Printer")
            return
        }
        
        printer.initPrinter()
        printer.process(["name"       :"米星互联网餐厅",
            "content"     :"米星，你的互联网餐厅",
            "servicePhone":"020-22332322",
            "orderNo"     :"12345",
            "deliveryTime":"2015-12-1 15:00",
            "serialNumber":"234235435234",
            "payType"     :1,
            "address"     :"海乐路合景睿峰2201",
            "phone"       :"020-232324343",
            "remark"      :"不要香菜",
            "contacts"    :"020-232332434",
            "detail"      :[["name":"大烤鸭", "number":2, "totalFee":10, "isDeliveryFee":false]],
            "totalFee":8888], logo: "logo.png", qrcode: "dnet_bitmap_QR.png")
        
        let bytes:[UInt8] = [0x1B, 0x4A, 0x99]
        let data:NSData = NSData(bytes: bytes, length: bytes.count)
        let sendData:BLESendData = BLESendData(CBCharacteristic: nil, cmdData: data)
        
        BLEManager.shareInstance().joinSendData(sendData)
        BLEManager.shareInstance().joinSendData(sendData)
        BLEManager.shareInstance().joinSendData(sendData)
        
        self.mz_showWaitOverlay()
        
        BLEManager.shareInstance().runSendDataQueue{ (error) -> Void in
            self.mz_removeAllOverlays()
        }

    }
    
    //MARK: Response Action
    @IBAction func scanButtonOnClick(sender: AnyObject) {
        let vc:UIViewController = self.storyboard!.instantiateViewControllerWithIdentifier("Devices")
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    @IBAction func connectButtonOnClick(sender: AnyObject) {
        BLEManager.shareInstance().disconnect()
    }
    
    
    @IBAction func test58OnClick(sender: AnyObject) {
        
        let printer = MazingPrinter58()
        testPrinter(printer)
    }
 
    @IBAction func test80OnClick(sender: AnyObject) {
 
        let printer = MazingPrinter80()
        testPrinter(printer)
    }
    
    @IBAction func showTip(sender: AnyObject) {
        showTip()
    }
}
